package com.ruoyi.training.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.training.domain.A04ResMajor;
import com.ruoyi.training.service.IA04ResMajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 课程库Controller
 * 
 * @author WorrilessGo
 * @date 2025-11-18
 */
@RestController
@RequestMapping("/major")
public class A04ResMajorController extends BaseController
{
    @Autowired
    private IA04ResMajorService a04ResMajorService;

    /**
     * 查询课程库列表
     */
    @RequiresPermissions("training:major:list")
    @GetMapping("/list")
    public TableDataInfo list(A04ResMajor a04ResMajor)
    {
        startPage();
        List<A04ResMajor> list = a04ResMajorService.selectA04ResMajorList(a04ResMajor);
        return getDataTable(list);
    }

    /**
     * 导出课程库列表
     */
    @RequiresPermissions("training:major:export")
    @Log(title = "课程库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, A04ResMajor a04ResMajor)
    {
        List<A04ResMajor> list = a04ResMajorService.selectA04ResMajorList(a04ResMajor);
        ExcelUtil<A04ResMajor> util = new ExcelUtil<A04ResMajor>(A04ResMajor.class);
        util.exportExcel(response, list, "课程库数据");
    }

    @PostMapping("/exportModel")
    public void exportModel(HttpServletResponse response)
    {
        List<A04ResMajor> list = new ArrayList<>();
        ExcelUtil<A04ResMajor> util = new ExcelUtil<A04ResMajor>(A04ResMajor.class);
        util.exportExcel(response, list, "课程库数据");
    }


    /**
     * 导入课程库数据
     */
    @RequiresPermissions("training:major:import")
    @Log(title = "课程库", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception{
        ExcelUtil<A04ResMajor> util = new ExcelUtil<A04ResMajor>(A04ResMajor.class);
        List<A04ResMajor> a04ResMajors = util.importExcel(file.getInputStream(), 1);
        int result = a04ResMajorService.batchInsertA04ResMajors(a04ResMajors);
        return toAjax(result);
    }

    /**
     * 获取课程库详细信息
     */
    @RequiresPermissions("training:major:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(a04ResMajorService.selectA04ResMajorById(id));
    }

    /**
     * 新增课程库
     */
    @RequiresPermissions("training:major:add")
    @Log(title = "课程库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody A04ResMajor a04ResMajor)
    {
        return toAjax(a04ResMajorService.insertA04ResMajor(a04ResMajor));
    }

    /**
     * 修改课程库
     */
    @RequiresPermissions("training:major:edit")
    @Log(title = "课程库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody A04ResMajor a04ResMajor)
    {
        return toAjax(a04ResMajorService.updateA04ResMajor(a04ResMajor));
    }

    /**
     * 删除课程库
     */
    @RequiresPermissions("training:major:remove")
    @Log(title = "课程库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(a04ResMajorService.deleteA04ResMajorByIds(ids));
    }
}
